﻿/*
 * @Author: huang yuanfa
 * @Date: 2022-11-13 23:06:44
 * @LastEditTime: 2022-12-11 11:50:31
 * @LastEditors: huang yuanfa
 * Copyright 2022 huang yuanfa All Rights Reserved.
 * @Gitee: https://gitee.com/huangyuanfa
 * @Github: https://github.com/hyf888
 * @FilePath: /HThreadPool/src/Utils/ThreadPool/ThreadPool.h
 * @Description: file content
 */
#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__

#include <memory>
#include "CoreThread.h"
#include "DaemonThread.h"
#include "HNoCopyable.h"
#include "UtilsObject.h"
#include "export.h"
#include "scheduler.h"

HTHREADPOOL_NAMESPACE_BEGIN

    class NEBULAWEAVER_EXPORT ThreadPool : public UtilsObject
    {
    public:
        explicit ThreadPool() noexcept;
        ~ThreadPool();
        template<int DefaultStrategy = -1, typename F, typename... Args>
        auto commit(F&& f, Args&&... args) -> std::future<decltype(f(args...))>;

        template<typename Func>
        auto commitWithPriority(const Func& func, int priority) -> std::future<typename std::result_of<Func()>::type>;

        void init();

    private:
        void        monitor();
        ThreadPool& operator=(const ThreadPool&) = delete;
        ThreadPool(const ThreadPool&)            = delete;

    private:
        std::shared_ptr<DaemonThread>            m_daemonThread;
        std::vector<std::unique_ptr<CoreThread>> m_coreThreads;
        Scheduler                                m_scheduler;
        AtomicQueue<Task>                        m_taskQueue;
    };

    using ThreadPoolPtr = ThreadPool*;

HTHREADPOOL_NAMESPACE_END
#include "ThreadPool.inl"
#endif
